package com.andnnl.map;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.stream.Collectors.toList;

/**
 * Created by chenss on 2021/11/19.
 */
public class SimpeEntityTest {
    public static void main(String[] args) {
        Student student1 = new Student("Johnson",UUID.randomUUID());
        Student student2 = new Student("Lily",UUID.randomUUID());
        Student student3 = new Student("KangKang",UUID.randomUUID());
        //创建
        Map<UUID, Student> collect1 = Stream.of(new AbstractMap.SimpleEntry<>(student1.getSid(), student1)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        Map<UUID, List<Student>> collect2 = Stream.of(new AbstractMap.SimpleEntry<>(student1.getSid(), newArrayList(student1))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        Map<UUID, Map<UUID, Student>> collect3 = Maps.newHashMap();
        System.out.println(JSONUtil.toJsonStr(collect1));
        System.out.println(JSONUtil.toJsonStr(collect2));
        System.out.println(JSONUtil.toJsonStr(collect3));
        //合并（添加）
        collect1.merge(student2.getSid(), student2, (oldValue, value) -> value.getSid().compareTo(oldValue.getSid())==0 ? value : oldValue);
        collect2.merge(student2.getSid(), newArrayList(student2), (oldValue, value) -> Stream.concat(oldValue.stream(), value.stream()).collect(toList()));
        collect3.merge(student3.getSid(),collect1,(oldValue,value)->{
            oldValue.putAll(value);
            return oldValue;
        });

        System.out.println(collect1);
    }

    static class Student{
        private String name;
        private UUID sid;

        public Student(String name, UUID sid) {
            this.name = name;
            this.sid = sid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public UUID getSid() {
            return sid;
        }

        public void setSid(UUID sid) {
            this.sid = sid;
        }
    }
}
